from time import sleep

from selenium.webdriver.common.by import By

from Common.Utils import BlogDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Interaction:
    url=""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:58080/sign-in.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    def register(self):
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a").click()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("@xiaohai")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("@xiaohai")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("@123Ab")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("@123Ab")
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

    def Userinteraction(self):

        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("@xiaohai")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("@123Ab")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)

        browse = self.driver.find_element(By.CSS_SELECTOR,
                                          "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)"
                                          ).text

        like = self.driver.find_element(By.CSS_SELECTOR,
                                        "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)"
                                        ).text
        talk = self.driver.find_element(By.CSS_SELECTOR,
                                        "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)"
                                        ).text
        #进入详情页
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"
                                 ).click()



        # 浏览量变化测试
        new_browse = self.driver.find_element(By.CSS_SELECTOR,
                                              "#details_article_visitCount"
                                              ).text
        # print(f"{int(browse) + 1}")
        # print(f"{new_browse}")
        assert int(new_browse) == int(browse) + 1, print(f"浏览数量不一致，旧的数量为{browse}，新的为{new_browse}")
        BlogDriver.getScreenshort()
        # 点赞测试
        self.driver.find_element(By.CSS_SELECTOR, "#details_btn_like_count").click()

        sleep(2)
        new_like = self.driver.find_element(By.CSS_SELECTOR,
                                            "#details_article_likeCount"
                                            ).text

        assert int(new_like) == int(like) + 1, print(f"点赞数量不一致，旧的数量为{like}，新的为{new_like}")
        BlogDriver.getScreenshort()
        #回复数量测试
        new_talk = [talk.text for talk in self.driver.find_elements(
            By.CSS_SELECTOR, "#details_reply_area .markdown-body p"
        )]
        new_talk = len(new_talk)
        assert int(talk) == int(new_talk), print(f"回复数量不一致，旧的数量为{talk}，新的为{new_talk}")
        sleep(2)
        BlogDriver.getScreenshort()
        self.driver.execute_script("window.scrollTo(0, 400);")
        sleep(1)
        BlogDriver.getScreenshort()

        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-3.card > div > h3").click()

        sleep(2)

        elements_to_check = [
            ("#profile_nickname", "小明"),
            ("#profile_remark", "你们好，我是叫小明！"),
            ("#profile_email", "123@qq.com"),

        ]

        # 循环检查所有元素
        for selector, expected_text in elements_to_check:
            try:
                element = WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.CSS_SELECTOR, selector))
                )
                actual_text = element.text
                print(f"验证元素 [{selector}] -> 实际内容: {actual_text}")

                # 断言并附带清晰错误信息
                assert actual_text == expected_text, \
                    f"文本不匹配！期望: '{expected_text}'，实际: '{actual_text}'"

            except AssertionError as e:
                print(f"\u274C 断言失败: {str(e)}")
                # 这里可以添加失败截图等操作
            except Exception as e:
                print(f"\u26A0\uFE0F 发生意外错误: {str(e)}")


        #测试帖子数量
        sleep(1)
        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#profile_articleCount"
        ).text
        old_count = int(old_count)

        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#profile_article_body li .text-truncate a strong")]

        count = len(post_titles)
        print(f"数量正确，数量为{count}")
        assert count == old_count, f"数量不符合, 数量为 {old_count}, 实际为 {count}"

        #私信设置
        sleep(2)
        # 他人中心发送私信
        self.driver.find_element(By.XPATH,"//*[@id='btn_profile_send_message']").click()

        sleep(2)
        #取消发送
        self.driver.find_element(By.XPATH, "//*[@id='index_message_receive_content']").send_keys("站内信取消发送")
        BlogDriver.getScreenshort()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_receive_cancel").click()


        #他人中心确认发送私信
        self.driver.find_element(By.XPATH, "//*[@id='btn_profile_send_message']").click()
        sleep(1)
        self.driver.find_element(By.XPATH, "//*[@id='index_message_receive_content']").send_keys("他人中心私信发送")
        sleep(1)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message").click()
        sleep(1)
        BlogDriver.getScreenshort()

        #个人中心浏览量统计
        browse = self.driver.find_element(By.CSS_SELECTOR,
                                          "#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)"
                                          ).text

        # #贴主私信发送测试
        self.driver.find_element(By.CSS_SELECTOR, "#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate > a > strong").click()

        new_browse = self.driver.find_element(By.CSS_SELECTOR,
                                          "#details_article_visitCount"
                                          ).text



        assert int(new_browse)==int(browse)+1

        sleep(1)
        #取消发送
        self.driver.find_element(By.CSS_SELECTOR, "#btn_details_send_message").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_content").send_keys("楼主回复取消测试")
        sleep(1)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_cancel").click()

        # 确定发送
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#btn_details_send_message").click()
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_content").send_keys("楼主回复私信")
        sleep(1)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message").click()
        sleep(1)
        BlogDriver.getScreenshort()




        new_browse = self.driver.find_element(By.CSS_SELECTOR,
                                          "#details_article_visitCount"
                                          ).text
        assert int(new_browse)==int(browse)+1

        # 帖主回复测试
        sleep(1)
        self.driver.execute_script("window.scrollBy(0,400);")
        sleep(2)
        #取消发送
        self.driver.find_element(By.CSS_SELECTOR, "#details_reply_area > div:nth-child(2) > div.col-3.card > div > div > a").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_content").send_keys("回复取消测试")
        sleep(1)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_cancel").click()

        # 确定发送
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#details_reply_area > div:nth-child(2) > div.col-3.card > div > div > a").click()
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_receive_content").send_keys("贴主回复私信")
        sleep(1)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message").click()
        sleep(1)
        BlogDriver.getScreenshort()

        #退出账户
        self.driver.execute_script("window.scrollBy(0,-800);")
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_logout").click()

        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("小ming")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #查看私信消息
        sleep(1)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg").click()
        sleep(1)
        BlogDriver.getScreenshort()


        #楼主回复私信

        sleep(2)
        user=self.driver.find_element(By.XPATH, "//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[2]/strong").text

        assert user=="@xiaohai"

        content = self.driver.find_element(By.XPATH,
                                    "//*[@id='index_div_message_list']/div[1]/div/div[2]/div").text

        assert content=="贴主回复私信"


        #贴主回复私信
        sleep(2)
        user=self.driver.find_element(By.XPATH, "//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[2]/strong").text

        assert user=="@xiaohai"

        content = self.driver.find_element(By.XPATH,
                                    "//*[@id='index_div_message_list']/div[2]/div/div[2]/div").text

        assert content=="楼主回复私信"


        #他人中心私信
        sleep(2)
        user=self.driver.find_element(By.XPATH, "//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[2]/strong").text

        assert user=="@xiaohai"

        content = self.driver.find_element(By.XPATH,
                                    "//*[@id='index_div_message_list']/div[3]/div/div[2]/div").text

        assert content=="他人中心私信发送"

        #私信状态
        state=self.driver.find_element(By.XPATH,"//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[1]").text
        assert state=="[未读]"
        sleep(1)

        #取消发送

        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,
                             "#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a").click()
        sleep(1)

        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_message_reply").click()

        self.driver.find_element(By.CSS_SELECTOR, "#index_message_reply_receive_content").send_keys("取消私信回复")

        sleep(1)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR,
                             "#index_message_reply_cancel").click()
        sleep(1)


        #状态确认
        state = self.driver.find_element(By.XPATH, "//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[1]").text
        assert state=="[已读]"


        #确认发送
        self.driver.find_element(By.CSS_SELECTOR, "#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_message_reply").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_message_reply_receive_content").send_keys("私信回复")
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message_reply").click()
        #消息状态获取
        state = self.driver.find_element(By.XPATH, "//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[1]").text
        assert state == "[已回复]"
        #退出账户
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_offcanvasEnd > div.offcanvas-header > button").click()
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_logout").click()

        #登录私信用户
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("@xiaohai")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("@123Ab")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        BlogDriver.getScreenshort()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg").click()
        sleep(1)
        BlogDriver.getScreenshort()
        #确认消息
        user=self.driver.find_element(By.XPATH, "//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[2]/strong").text

        assert user=="小明"

        state = self.driver.find_element(By.XPATH, "//*[@id='index_div_message_list']/div[1]/div/div[2]/a/span[1]").text
        assert state == "[未读]"

        content = self.driver.find_element(By.XPATH,
                                           "//*[@id='index_div_message_list']/div[1]/div/div[2]/div").text

        assert content == "私信回复"
        sleep(1)
        BlogDriver.getScreenshort()








        # self.driver.find_element(By.CSS_SELECTOR, "").click()






















